package com.ColonyCount.View;

import javax.microedition.khronos.opengles.GL;
import javax.microedition.khronos.opengles.GL10;

import com.ColonyCount.Vector2f;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.graphics.Region;

public class ROISphere extends ROIBase {
	
	RectF bounds = new RectF();
	
	static final int ControlPoint_TopLeft = 0;
	static final int ControlPoint_TopRight = 1;
	static final int ControlPoint_BottomLeft = 2;
	static final int ControlPoint_BottomRight = 3;
	static final int ControlPoint_Count = 4;
	
	int controlPoint = -1;
	
    public ROISphere(float density, RectF bounds)
    {
    	super(density);
    	this.bounds = bounds;
    }
    
    @Override
	public void drawFilterPreview(Canvas canvas)
	{
    	// Clamp bounds
    	bounds.left = Math.max(bounds.left, 0);
    	bounds.right = Math.min(bounds.right, canvas.getWidth());
    	bounds.top = Math.max(bounds.top, 0);
    	bounds.bottom = Math.min(bounds.bottom, canvas.getHeight());
    	
    	// Gray out
    	canvas.save();

    	Path clipPath = new Path();
		clipPath.addOval(bounds,  Path.Direction.CCW);
		canvas.clipPath(clipPath, Region.Op.DIFFERENCE);

		Paint paint = new Paint();
		paint.setColor(0x66000000);
//		float[] array = { 	
//				0, 0, 0, 0, 0, 
//				0, 0, 0, 0, 0, 
//				0, 0, 1, 0, 0, 
//				0, 0, 0, 0, 100 };
//		ColorMatrix colorMatrix = new ColorMatrix(array);
//		paint.setColorFilter(new ColorMatrixColorFilter(colorMatrix));
		paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC));
		canvas.drawPaint(paint);

		
		
		
		canvas.restore();
	}
    
	public void drawFilterGizmoes(Canvas canvas)
	{
    	// Sphere
    	Paint regionPaint = new Paint();
    	regionPaint.setStyle(Paint.Style.STROKE);
    	regionPaint.setColor(Color.BLACK);
    	regionPaint.setStrokeWidth(2.0f);
    	canvas.drawOval(bounds, regionPaint);
		
		// Handles
    	drawControlPoint(canvas, ControlPoint_TopLeft);
    	drawControlPoint(canvas, ControlPoint_TopRight);
    	drawControlPoint(canvas, ControlPoint_BottomLeft);
    	drawControlPoint(canvas, ControlPoint_BottomRight);
	}
	
    @Override
	public boolean select(float x, float y)
	{
    	for(int i=0;i<ControlPoint_Count;i++)
    	{
    		if(insideControlPoint(x, y, i)) 
    		{
    			controlPoint = i;
    			return true;
    		}
    	}
		return false;
	}
	
	@Override
	public void move(float deltaX, float deltaY)
	{
		switch(controlPoint)
		{
		case ControlPoint_TopLeft:
			bounds.left += deltaX;
			bounds.top += deltaY;
			break;
		case ControlPoint_TopRight:
			bounds.right += deltaX;
			bounds.top += deltaY;
			break;
		case ControlPoint_BottomLeft:
			bounds.left += deltaX;
			bounds.bottom += deltaY;
			break;
		case ControlPoint_BottomRight:
			bounds.right += deltaX;
			bounds.bottom += deltaY;
			break;
		}
		
	}
    
    @Override
	public RectF getBounds()
	{
		return bounds;
	}
    
    private boolean insideControlPoint(float pointX, float pointY, int controlPoint)
    {
		Vector2f controlPos = getControlPointPos(controlPoint);

		float distX = Math.abs(pointX - controlPos.x);
		float distY = Math.abs(pointY - controlPos.y);
		if(distX < controlSize && distY < controlSize)
		{
			return true;
		}
    	return false;
    }
    
	private void drawControlPoint(Canvas canvas, int controlPoint)
	{
		Paint paint = new Paint();
		paint.setColor(Color.RED);
		paint.setStyle(Paint.Style.STROKE);
		paint.setStrokeWidth(2.0f);

		Vector2f controlPos = getControlPointPos(controlPoint);
		float radius = controlSize*0.5f;
    	RectF box = new RectF(controlPos.x - radius, controlPos.y - radius, controlPos.x + radius, controlPos.y + radius);
    	canvas.drawOval(box, paint);
	}
	
	private Vector2f getControlPointPos(int controlPoint)
	{
		float width = bounds.right - bounds.left;
		float height = bounds.bottom - bounds.top;

		float invCos45 = 0.292893f;
		
		float deltaX = width*0.5f*invCos45;
		float deltaY = height*0.5f*invCos45;
		
		switch(controlPoint)
		{
			case ControlPoint_TopLeft: return new Vector2f(bounds.left + deltaX, bounds.top + deltaY);
			case ControlPoint_TopRight: return new Vector2f(bounds.right - deltaX, bounds.top + deltaY);
			case ControlPoint_BottomLeft: return new Vector2f(bounds.left + deltaX, bounds.bottom - deltaY);
			case ControlPoint_BottomRight: return new Vector2f(bounds.right - deltaX, bounds.bottom - deltaY);
		}
		return null;
	}
}
